/**
 * 
 */
package com.yidu.erp.action.sell;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.common.SsmMessage;
import com.yidu.common.Tools;
import com.yidu.erp.model.BeyondFdstock;
import com.yidu.erp.model.BeyondFinancialIncome;
import com.yidu.erp.model.BeyondSctockmp;
import com.yidu.erp.model.BeyondStaff;
import com.yidu.erp.service.sell.BeyondFdstockService;

/**
 * 分店销售
 * @author WangGuanfu
 * 
 */
@Controller
@RequestMapping("sell")
public class BeyondSell {
	
	Logger logger = Logger.getLogger(BeyondSell.class);
	@Resource
	private BeyondFdstockService beyondFdstockService;
	
	/**
	 * 显示销售的商品
	 * @return
	 */
	@RequestMapping("selectbyWareId")
	@ResponseBody
	public List<Map<String, Object>> selectbyWareId(HttpSession session){
		//获取登录的session
		BeyondStaff beyondStaff = (BeyondStaff) session.getAttribute("staff");
		Map<String, Object> map = new HashMap<String,Object>();
		//将分店ID传入map
		map.put("annexId", beyondStaff.getAnnexId());
		//将员工ID传入map
		map.put("staffId", beyondStaff.getStaffId());
		//显示分店销售的商品
		List<Map<String, Object>> beyond=beyondFdstockService.selectbyWareId(map);
		return beyond;
	}

	/**
	 * 增加分店销售，销售明细，修改库存，增加财务收入，修改财务金额
	 * @param str
	 * @param session
	 * @param bf
	 * @param bs   
	 * @param bfl
	 * @return  
	 */
	@RequestMapping("update")
	@ResponseBody
	public SsmMessage update(String str , HttpSession session , BeyondFdstock bf , BeyondSctockmp bs , BeyondFinancialIncome bfl) {
		//获取登录的session
		BeyondStaff beyondStaff = (BeyondStaff) session.getAttribute("staff");
		//得到消息类的对象
		SsmMessage mes = new SsmMessage();
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> map1 = new HashMap<>();
		Map<String, Object> map2 = new HashMap<>();
		String[] sourceStrArray = str.split("&");//字符串分割
		
		int  count = 0;//定义一个数量
		int price  =0;//定义一个金额
		//增加订单
		//循环数组
		for(int i=0 ; i<sourceStrArray.length;i++){
			//字符串分割
			String[] str3 = sourceStrArray[i].split("_");
			//循环相加数量3
			count = count+Integer.valueOf(str3[3]);
			//循环相加金额4
			price = price+Integer.valueOf(str3[4]);
		}
		String  saleId = Tools.getRandomString(11);//生成随机字符串
		//讲价格字符串转换成大数据类型
		BigDecimal bd = new BigDecimal(price);
		//将分店ID赋值到分店销售
		bs.setAnnexId(beyondStaff.getAnnexId());
		//将人员ID赋值到分店销售
		bs.setStaffId(beyondStaff.getStaffId());
		//将总数量赋值到分店销售
		bs.setSaleNum(count);
		//将总金额D赋值到分店销售
		bs.setSaleMoney(bd);
		//将销售id赋值到分店销售
		bs.setSaleId(saleId);
		//增加销售
		beyondFdstockService.insertSelective(bs);
		
		//订单明细增加
		for(int i =0;i<sourceStrArray.length;i++){
			String  bfId = Tools.getRandomString(10);//随机生成销售明细ID
			//得到日期的对象
			Date date = new Date();
			//字符串分割
			String[] str3 = sourceStrArray[i].split("_");
			//将销售金额转换成大数据类型1
			BigDecimal bd1 = new BigDecimal(str3[1]);
			//将销售明细ID赋值到销售明细
			bf.setFdId(bfId);
			//将销售ID赋值到销售明细
			bf.setSaleId(saleId);
			//将金额赋值到销售明细
			bf.setFdSum(bd1);
			//将数量赋值到销售明细3
			bf.setFdNumber(Integer.valueOf(str3[3]));
			//将商品ID赋值到销售明细0
			bf.setWareId(str3[0]);
			//将时间赋值到销售明细
			bf.setFdTime(date);
			//增加销售明细
			beyondFdstockService.insertSelectiveFdId(bf);
			//将商品ID传入map0
			map.put("wareId", str3[0]);
			//将购买数量传入map3
			map.put("inventoryNum", str3[3]);
			//修改库存
			beyondFdstockService.update(map);
		}
		//将分店ID传入map
		map1.put("annexId", beyondStaff.getAnnexId());
		//根据分店ID查询财务id
		int  moneyId = beyondFdstockService.selectMoneyById(map1);
		
		bfl.setMoneyId(String.valueOf(moneyId));
		bfl.setAnnexId(beyondStaff.getAnnexId());
		bfl.setInMoney(bd);
		bfl.setInState("");
		bfl.setInRemark("销售收入");
		//增加财务收入表
		beyondFdstockService.insertSelective(bfl); 
		//将价格传入map
		map2.put("price", price);
		//将分店ID传入map
		map2.put("annexId", beyondStaff.getAnnexId());
		//修改财务
	    int row =beyondFdstockService.updateMoney(map2);
	    if(row>0){
		    mes.setState(1);
	    }else if(row<=0){
	    	 mes.setState(0);
	    }
		return mes;
	}
	
	
}